/*
 * ========THE SOLMIX PROJECT=====================================
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */
package org.solmix.fmk.config.impl;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.solmix.commons.util.DataUtil;
import org.solmix.fmk.config.ConfigRealm;
import org.solmix.fmk.config.ConfigurationEntry;
import org.solmix.fmk.config.ConfigurationManager;

/**
 * 
 * @version $Id$
 */
public class OsgiConfiguration implements ConfigurationManager
{
   private static final List<ConfigRealm> realms = new CopyOnWriteArrayList<ConfigRealm>();

   public void init()
   {
      System.out.println("===============================init OsigConfiguration========");

   }

   public void close()
   {
      realms.clear();
   }

   public void register(ConfigRealm realm)
   {
      realms.add(realm);
   }

   public void unregister(ConfigRealm realm)
   {
      realms.remove(realm);
   }

   public List<ConfigurationEntry> getAppConfigurationEntry(String name)
   {
      ConfigRealm realm = null;
      for (ConfigRealm r : realms) {
         if (r.getName().equals(name)) {
            if (realm == null || r.getRank() > realm.getRank()) {
               realm = r;
            }
         }
      }
      if (realm != null) {
         return realm.getEntries();
      }
      return null;
   }

   public void refresh()
   {
      // Nothing to do, as we auto-update the configuration
   }

   /**
    * {@inheritDoc}
    * 
    * @see org.solmix.fmk.config.ConfigurationManager#getRealmCount()
    */
   @Override
   public int getRealmCount()
   {
      
      return realms == null ? 0 : realms.size();
   }

   /**
    * {@inheritDoc}
    * 
    * @see org.solmix.fmk.config.ConfigurationManager#getRealms()
    */
   @Override
   public List<ConfigRealm> getRealms()
   {
      return realms;
   }

   /**
    * {@inheritDoc}
    * 
    * @see org.solmix.fmk.config.ConfigurationManager#getRealm(java.lang.String)
    */
   @Override
   public ConfigRealm getRealm(String realmName)
   {
      if (DataUtil.isNotNullAndEmpty(realms))
      {
         for (ConfigRealm realm : realms)
         {
            if (realm.getName().equals(realmName))
            {
               return realm;
            }
         }
      }
      return null;
   }

   /**
    * {@inheritDoc}
    * 
    * @see org.solmix.fmk.config.ConfigurationManager#getConfigEntries(java.lang.String)
    */
   @Override
   public List<ConfigurationEntry> getConfigEntries(String realmName)
   {
      ConfigRealm realm =getRealm( realmName);
      if(realm!=null){
         return realm.getEntries();
      }
      return null;
   }

   /**
    * {@inheritDoc}
    * 
    * @see org.solmix.fmk.config.ConfigurationManager#getConfigEntry(java.lang.String, java.lang.String)
    */
   @Override
   public ConfigurationEntry getConfigEntry(String realmName, String moduleName)
   {
      List<ConfigurationEntry> entries =getConfigEntries(realmName);
      for(ConfigurationEntry entry: entries){
         if(moduleName.equals(entry.getModuleName())){
            return entry;
         }
      }
      return null;
   }

   /**
    * {@inheritDoc}
    * 
    * @see org.solmix.fmk.config.ConfigurationManager#getConfigEntryCount(java.lang.String)
    */
   @Override
   public int getConfigEntryCount(String realmName)
   {
      ConfigRealm realm = this.getRealm(realmName);
      if(realm!=null){
         return realm.getEntries()==null?0:realm.getEntries().size();
      }else{
         return 0;
      }
   }

}
